﻿using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Microsoft.Practices.ServiceLocation;
using TimeTravel.Controllers;
using TimeTravel.Extensions;
using TimeTravel.Extensions.XmlRouting;
using TimeTravel.Extentions;
using TimeTravel.IServices;
using TimeTravel.Commons;
using TimeTravel.Components;

namespace TimeTravel.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
            InitializeServiceLocator();
            InitializeLogger();
        }
        
        protected void Session_Start()
        {
            RecordVistorInfo();
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            var ex = Server.GetLastError();
            if (ex != null && ex.Message.IndexOf("0x80070459", StringComparison.OrdinalIgnoreCase) < 0)
            {
                LogHelper<MvcApplication>.Log(ex);
            }
        }

        #region SomeMethods
        protected void RecordVistorInfo()
        {
            var visitorService = ServiceLocator.Current.GetInstance<IVisitorService>();
            var visitor = new VisitorInfo
                              {
                                  Browser = HttpHelper.GetBrowser(),
                                  ClientIP = HttpHelper.GetClientIPAddress(),
                                  Date = DateHelper.GetTimeStampFromDate(DateTime.Now),
                                  Source = HttpHelper.GetUrlReferrer()
                              };
            //visitorService.Add(visitor);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            var section = (MvcRouteConfigurationSection)ConfigurationManager.GetSection("RouteConfiguration");
            routes.MapRoute(section);
            routes.RouteExistingFiles = true;
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }

        protected virtual void InitializeLogger()
        {
            const string logConfigPath = "~/Configuration/Log4net.config";
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath(logConfigPath)));
        }

        protected virtual void InitializeServiceLocator()
        {
            IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
            ControllerBuilder.Current.SetControllerFactory(new ChannelControllerFactory(container));
            ComponentRegister.AddComponentsTo(container);
            ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
        } 
        #endregion
    }
}